<template>
    <div style="height: 100%; background-color: #f9f8f7">
      <div class="topBtns">
        <span v-if="pageIndex!=0" class="back el-icon-arrow-left" @click="back">
        </span>
        {{topBtnsTitles[pageIndex]}}
      </div>
      <GeminiScrollbar :style="{height:(windowHeight - 50)+'px'}">
      <div v-if='pageIndex===0'>
      <div class="aSingleMsg" style="background-color: #e4e4e4" @click="toFriendRequestPage">
        <div class="avatar">
        </div>
        <span class="friendName">消息通知</span>
        <span class="time">     </span><br>
        <span v-if="friendRequests.length!=0" class="content">查看通知</span>
        <span v-else class="content">没有通知</span>
      </div>

        <!--        消息列表-->
        <div v-for="(item, index) in allMessages" class="aSingleMsg" @click="toMessageCard(item.targetId, index)">
          <div class="avatar">
            <el-avatar :size="50" :src="getFriendAvatar(item.targetId)"></el-avatar>
          </div>
          <span class="friendName">{{getFriendName(item.targetId)}}</span>
          <span class="time">{{getTime(item.messages[item.messages.length-1].time)}}</span><br>
          <span class="content">{{item.messages[item.messages.length-1].content}}</span>
          <el-badge class="mark" v-if="item.unReadCount!=0" :value="item.unReadCount"></el-badge>
        </div>
      </div>
<!--      这个页面是好友申请列表-->
      <div v-if="pageIndex===1">
          <div class="aSingleMsg" v-for="(item, index) in friendRequests" v-if="friendRequests!=null">
            <div class="avatar">
              <el-avatar :size="50" :src="item.tagUserInfo.avatar"></el-avatar>
            </div>
            <span class="friendName">{{item.tagUserInfo.username}}</span>
            <span class="time">{{item.time}}</span><br>
            <span class="content">{{item.content}}</span>
            <span class="friendResOptions" v-if="item.type==='receive'">
              <el-button v-if="item.options==='none'" size="small" plain type="primary" round @click="agreeRequest(item,index)">同 意</el-button>
              <el-button v-if="item.options==='none'" size="small" round plain type="danger" @click="rejectRequest(item,index)">拒 绝</el-button>
              <span v-if="item.options==='agreement'" size="small" round style="font-size: 14px; color: #99a9bf; margin-right: 10px;">已同意</span>
              <span v-if="item.options==='reject'" size="small" round style="font-size: 14px; color: #99a9bf; margin-right: 10px;">已拒绝</span>
            </span>
            <span class="friendResOptions" v-if="item.type==='send'">
              <span v-if="item.options==='none'" size="small" round style="font-size: 14px; color: #99a9bf; margin-right: 10px;">等待验证</span>
              <span v-if="item.options==='agreement'" size="small" round style="font-size: 14px; color: #99a9bf; margin-right: 10px;">通过验证</span>
              <span v-if="item.options==='reject'" size="small" round style="font-size: 14px; color: #99a9bf; margin-right: 10px;">被拒绝</span>
            </span>
          </div>
      </div>
      </GeminiScrollbar>
    </div>
</template>

<script>
  import timeFormat from './TimeFormat'
  export default {
    name: 'MessageList',
    props:['friendRequests', 'stompClient','allMessages', 'windowHeight','friendList'],
    data(){
      return{
        records: null,
        pageIndex: 0,
        topBtnsTitles: ['消息', '好友通知'],
        dialogVisible: false,
        dialogContent: {
          content:'',
          avatar:'',
          state:'',
          confirmBtn: '确定',
          // friendList: []

    }
      }
    },
    methods:{
      // 跳转到好友申请消息
      toFriendRequestPage(){
        this.pageIndex = 1;
      },
      // 返回上一级目录
      back(){
        this.pageIndex--;
      },
      // 同意申请
      agreeRequest(item,index){
        this.stompClient.send('/app/addFriendRequest', {},
          JSON.stringify({uid: window.localStorage.uid, toId: item.tagId, type: 'response', content: 'agreement'}))
        this.friendRequests[index].options = 'agreement'
      },
      // 拒绝消息
      rejectRequest(item, index){
        this.stompClient.send('/app/addFriendRequest', {},
          JSON.stringify({uid: window.localStorage.uid, toId: item.tagId, type: 'response', content: 'reject'}))
        this.friendRequests[index].options = 'reject'
      },
      // 获取用户的昵称
      getFriendName(tagid){
        console.log('tagid:' + tagid);
        for(var i = 0; i<this.friendList.length; i++){
          if(this.friendList[i].uid === tagid)
            return this.friendList[i].username
        }
      },
      // 获取用户列表的头像
      getFriendAvatar(tagid){
        for(var i = 0; i < this.friendList.length; i++){
          if(this.friendList[i].uid === tagid) {
            return this.friendList[i].avatar
          }
        }
      },
      toMessageCard(tagId, index){
        this.allMessages[index].unReadCount = 0
        var i
        for(i = 0; i<this.friendList.length; i++){
          if(this.friendList[i].uid ===tagId)
            break;
        }
        // 将请求发给父类，让父类操作
        this.$emit('changeMessageCard', this.friendList[i]);
        // 消除未读消息
        this.$http(this.$api.clearUnreadCount, {uid: window.localStorage.uid,tagId:tagId}).then(res=>{
          console.log("消除未读消息")
          console.log(res)
        })
      },
      getTime(time){
        return timeFormat(time)
      }
    },
    created () {
      if (window.localStorage.records === undefined)
        this.records = null
      else
        this.records = JSON.parse(window.localStorage.records)
        // this.friendList = JSON.parse(window.localStorage.friendList)
    },
    watch: {
      windowHeight (val, oldVal) {
        this.inputAreaAutoSize = {
          minRows: Number((val * 0.3 - 110) / 23)
        }
      },
    },
  }
</script>
<style scoped>
  .aSingleMsg{
    height: 50px;
    text-align: left;
    position: relative;
    padding: 10px;
    border-style: solid;
    border-color: #EBEEF5;
    border-width: 1px 0 1px 0;
    overflow-y: hidden;
  }
  .content{
    color: #8c939d;
    font-size: 15px;
    overflow: hidden;
    width: 10%;
    line-height: 35px;
  }
  .avatar{
    height: 100%;
    width: 50px;
    margin-right: 10px;
    display: inline-block;
    float: left;
  }
  .mark{
    right: 30px;
    position: absolute;
    top: 30px;
  }
  .time{
    font-size: 12px;
    color: #99a9bf;
    position: absolute;
    right: 40px;
    top: 10px;
  }
  .topBtns{
    background-color: #534e44;
    height: 30px;
    width: 100%;
    padding: 10px;
    color: white;
    font-weight: bold;
    font-size: 18px;
  }
  .back{
    position: absolute;
    left: 10px;
    top: 14px;
  }
  .friendResOptions{
    right: 30px;
    top: 25px;
    position: absolute;
  }
</style>
